const {
  $util,
} = getApp();
Page({

  /**
   * 页面的初始数据 
   */
  data: {
    title: '',
    content: '',
    timelimit: [],
    address: '请选择考察地点',
    map: '',
    latitude: '',
    longitude: '',
    target_obj: '', // 党员id
    nowDate: '',
    startDate: '',
    endDate: '',
    startTime: '',
    endTime: '',
    isLoading: false,
    gtype: 0,
    groupname: '',
    imgUrl: $util.getImgUrl(),
  },
 
  startDateChange: function (e) {
    // 选择开始日期的时候 将结束日期时间默认为同一天
    this.setData({
      startDate: e.detail.value,
    });

    let _startTime = this.data.startDate + ' ' + this.data.startTime;
    this.setData({
      endDate: getApp().util.formateDateTime((new Date(_startTime).getTime() + 60 * 60 * 1000) / 1000),
      endTime: getApp().util.format(new Date(_startTime).getTime() + 60 * 60 * 1000, 'hh:mm'),
    });
    wx.setStorageSync('mettingTime', {
      endDate: this.data.endDate,
      endTime: this.data.endTime,
      startDate: this.data.startDate,
      startTime: this.data.startTime,
    });
  },
  endDateChange: function (e) {
    // 选择开始时间的时候 将结束日期时间默认延迟一小时
    this.setData({
      endDate: e.detail.value,
    });
    wx.setStorageSync('mettingTime', {
      endDate: this.data.endDate,
      endTime: this.data.endTime,
      startDate: this.data.startDate,
      startTime: this.data.startTime,
    });
  },
  startTimeChange(e) {
    this.setData({
      startTime: e.detail.value,
    });

    let _startTime = this.data.startDate + ' ' + this.data.startTime;
    this.setData({
      endDate: getApp().util.formateDateTime((new Date(_startTime).getTime() + 60 * 60 * 1000) / 1000),
      endTime: getApp().util.format(new Date(_startTime).getTime() + 60 * 60 * 1000, 'hh:mm'),
    });

    wx.setStorageSync('mettingTime', {
      endDate: this.data.endDate,
      endTime: this.data.endTime,
      startDate: this.data.startDate,
      startTime: this.data.startTime,
    });

  },
  endTimeChange(e) {
    this.setData({
      endTime: e.detail.value,
    });
    wx.setStorageSync('mettingTime', {
      endDate: this.data.endDate,
      endTime: this.data.endTime,
      startDate: this.data.startDate,
      startTime: this.data.startTime,
    });
  },
  bindKeyInput: function (e) {
    let name = e.currentTarget.dataset.flag;

    if (name === 'title') {
      this.setData({
        title: e.detail.value,
      });
    }
    if (name === 'content') {
      this.setData({
        content: e.detail.value,
      });
    }

  },
  getAddress: function () {
    let that = this;
    wx.chooseLocation({
      type: 'gcj02', // 返回可以用于wx.openLocation的经纬度
      success(res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          address: res.address,
          map: res.latitude + ',' + res.longitude,
        });
      },
      fail(res) {
        that.getSetting();
      },
    });
  },
  getSetting() {
    let that = this;
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.userLocation']) {
          wx.openSetting({
            success: (res) => {
              if (res.authSetting['scope.userLocation']) {
                that.getAddress();
              }
            },
          });
        }
      },
    });
  },
  radioChange(e) {
    this.setData({
      gtype: parseInt(e.detail.value, 10),
    });
  },
  // 创建
  create: function () {
    this.setData({
      isLoading: true,
    });
    const {
      title,
      gtype,
      start,
      end,
      content,
      address,
      map,
      target_obj,
    } = this.data;
    let that = this;
    if (title.trim() === '') {
      wx.showModal({
        title: '提示',
        content: '请填写考察主题',
        showCancel: false,
      });
      return;
    }
    if (content.trim() === '') {
      wx.showModal({
        title: '提示',
        content: '请填写考察说明',
        showCancel: false,
      });
      return;
    }
    if (map.trim() === '') {
      wx.showModal({
        title: '提示',
        content: '请选择考察地点',
        showCancel: false,
      });
      return;
    }
    if (target_obj.trim() === '') {
      wx.showModal({
        title: '提示',
        content: '请选择考察人员',
        showCancel: false,
      });
      return;
    }
    wx.showModal({
      title: '提示',
      content: '确定要添加该考察吗？',
      success: (res) => {
        if (res.confirm) {
          $util.request({
            url: 'entry/wxapp/partyman.investigate.taskadd',
            data: {
              title,
              gtype,
              start: this.data.startDate + ' ' + this.data.startTime,
              end: this.data.endDate + ' ' + this.data.endTime,
              address,
              content,
              target_obj,
            },
            success: (res) => {
              if (res.errno === 0) {
                wx.removeStorageSync('addmeeting');
                wx.removeStorageSync('memberids');
                wx.navigateBack({});
              }
            },
            complete: function () {
              that.setData({
                isLoading: false,
              });
            },
          });
        }
      },
    });

  },
  onUnload: function () {
    wx.removeStorageSync('addmeeting');
    wx.removeStorageSync('memberids');
    wx.removeStorageSync('orgids');
  },
  bindOrgInput: function () {
    // 选择参会组织
    wx.navigateTo({
      url: '/ws_party_const/pages/organization/list',
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '党建考察',
    });
    this.loadUserInfo();
  },
  loadUserInfo() {
    $util.request({
      url: 'entry/wxapp/partyman.mine.index',
      data: {},
      success: (res) => {
        this.setData({
          groupname: res.groupname,
        });
      },
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    });

    let val = wx.getStorageSync('mettingTime');
    this.setData({
      nowDate: getApp().util.formateDateTime(new Date().getTime() / 1000),
      startDate: val.startDate || getApp().util.formateDateTime(new Date().getTime() / 1000),
      endDate: val.endDate || getApp().util.formateDateTime(new Date().getTime() / 1000),
      startTime: val.startTime || getApp().util.format(new Date(), 'hh:mm'),
      endTime: val.endTime || getApp().util.format(new Date().getTime() + 60 * 60 * 1000, 'hh:mm'),
    });

    // 获取选择的党员缓存信息
    if (wx.getStorageSync('addmeeting')) {
      let members = wx.getStorageSync('addmeeting');
      let ids = [];
      for (let i = 0; i < members.length; i++) {
        ids[i] = members[i].inid;
      }

      this.setData({
        members: members,
        target_obj: ids.join(','),
      });

    }

  },
});